<?php


namespace App\Http\Controllers\Mp;


use App\Http\Controllers\Controller;
use App\Http\Resources\PaginationCollection;
use App\Model\Deposit;
use App\Model\Device;
use App\Model\Repair;
use App\Utils\Result;
use Illuminate\Http\Request;

class DepositController extends Controller
{
    public function index(Request $request)
    {
        // Get the current page number from the request parameters (default to 1)
        $page = $request->input('page', 1);

        // Get the number of houses to show per page
        $perPage = 20;

        $query = Deposit::query();
        $query->where('business_id', $this->getBusinessId());

        $data = $query->with('house')
            ->orderByDesc('id')
            ->paginate($perPage, ['*'], 'page', $page);

        return Result::success(new PaginationCollection($data));
    }

    public function create(Request $request)
    {
        $data = $request->all();
        $res = new Deposit();
        $res->business_id = $this->getBusinessId();
        $res->fill($data);
        $res->save();

        return Result::success();
    }

    public function update(Request $request, $id)
    {
        $data = $request->all();
        $res = Deposit::find($id);
        $res->fill($data);
        $res->save();

        return Result::success();
    }

    public function detail($id)
    {
        $res = Deposit::with('house')->find($id);

        return Result::success($res);
    }
}
